angular.module('meHistoryListcontrollers', [])

  .controller('MeHistoryListCtrl', function ($scope,$ionicActionSheet,$ionicHistory, $state, $timeout, $ionicPopup, Storage, MeHistoryList,MeHistoryOrderDetail,Toast,Loading,ENV) {

    var memberId;
    $scope.ENV = ENV;

    $scope.searchText = "";
    //返回
    $scope.goback = function(){
      if(MeHistoryList.getIsGoBack2()){
        $ionicHistory.goBack(-2);
      }else {
        $ionicHistory.goBack(-1);
      }
    }
    //进入界面时，加载历史酒单数据
    var firstInHistory = true;
    $scope.$on('$ionicView.beforeEnter', function () {
      if(firstInHistory){
        Loading.showLoading();
        firstInHistory = false;
      }
      memberId = Storage.get("member").member_id;
      MeHistoryList.getHistoryWineList(memberId);
    });

    $scope.$on('Me.getHistoryWineList', function () {
      Loading.hideLoading();
      var result = MeHistoryList.getHistoryWineListJson();
      tyImgUrl = false;
      $scope.historyOrderList = result.historyOrderList;

    });

    $scope.changeSelectState = function (index) {
      var list = $scope.historyOrderList;
      for (var i = 0; i < list.length; i++) {
        if (list[i].mrw_id == index) {
          $scope.historyOrderList[i].selected = !$scope.historyOrderList[i].selected;
        }
      }
      // $scope.historyOrderList[index].selected = !$scope.historyOrderList[index].selected;
    };

    //点击全选
    $scope.selectAllOrders = function () {
      $scope.selectAllOrder = !$scope.selectAllOrder;
      if ($scope.selectAllOrder) {
        //将所有酒款都选中
        for (var i = 0; i < $scope.historyOrderList.length; i++) {
          $scope.historyOrderList[i].selected = true;
        }
      } else {
        //将所有酒款都去掉选中
        for (var j = 0; j < $scope.historyOrderList.length; j++) {
          $scope.historyOrderList[j].selected = false;
        }
      }
    };

    //删除历史酒单
    $scope.deleteOrders = function () {
      var getHistoryOrderList = $scope.historyOrderList;
      var deleteOrderIdList = [];
      for (var i = 0; i < getHistoryOrderList.length; i++) {
        if (getHistoryOrderList[i].selected) {
          deleteOrderIdList.push(getHistoryOrderList[i].mrw_id + "");
        }
      }
      if (deleteOrderIdList.length == 0) {
        Toast.showMessage("请选择要删除的酒单");
      } else {
        // 弹出确定窗口
        var confirmPopup = $ionicPopup.confirm({
          template: '<p style="text-align: center;font-size:16px">您确定要删除这些酒单么？</p>',
          cancelText: '取消',
          okText: '确定',
          okType: 'hd-red-btn',
          cancelType: 'cancel-esc-btn'
        });
        confirmPopup.then(function (res) {
          if (res) {
            //删除对应的酒单
            MeHistoryList.deleteOrders(memberId, deleteOrderIdList);
            $scope.selectAllOrder = false;
          }
        });
      }
    };

    //获取删除酒单后的返回结果
    $scope.$on('Me.deleteHistoryOrders', function () {
      var result = MeHistoryList.getDeleteOrdersJson();
      if (result.historyStatus) {
        $scope.historyOrderList = result.historyOrderList;
      } else {
        Toast.showMessage(result.historyMsg);
      }
    });


    //=======================暂时没有用到=========================
    // 根据关键字搜索
    $scope.searchWindOrder = function () {
      var text = $scope.searchText;
      MeHistoryList.searchWineOrderByName(text);
    };

    $scope.$on('Me.searchWineOrder', function () {
      var result = MeHistoryList.getSearchWineOrderByName();
    });
    //=======================暂时没有用到=========================


    //获取历史酒单详情
    $scope.getWineOrderDetail = function (mrw_id) {
      //设置历史酒单编号
      MeHistoryOrderDetail.setMrwId(mrw_id);
      //进入到历史酒单详情界面
      $state.go('history-order-detail');

    }


  });
